/* 获取元素位置 */
	function getpos(e) {
		var left = 0;
		var top  = 0;
		while (e.offsetParent) {
			left += e.offsetLeft;
			top  += e.offsetTop;
			e     = e.offsetParent;
		}
		left += e.offsetLeft;
		top  += e.offsetTop;
		return {left:left, top:top};
	}
	

	/* ----------------------------
		选择分类 - class
	---------------------------- */


	var sTime = "";

	function classes_open(obj, type, classid) {
		
		classes_keep();
		
		if(!$("body #context_menu").length) { $("body").append('<div id="context_menu" onmouseover="classes_keep()" onmouseout="classes_close()"></div>'); }	
		context_menu_xml = site_index+'admin/ajax/context_menu_classes/'+type+'/'+classid;
		
		var pos = getpos(obj);
		$('#context_menu').html('');
		$('#context_menu').load(context_menu_xml.replace(/ /g, "%20"));
		$("#context_menu").css("left", pos.left);
		$("#context_menu").css("top", pos.top + obj.offsetHeight - 1);
		$('#context_menu').show()
	}
	
	function classes_close() {

		if($("body #context_menu").length) { 
			classes_keep();
			sTime = setTimeout("$('#context_menu').hide('')", 500);
		}	
	}
	
	function classes_keep() { if(eval(sTime)) clearTimeout(sTime); }
	


	/* ----------------------------
		选择产品 - products
	---------------------------- */
	

	var context_menu_xml = "";
	var context_menu_objname = "";
	var context_menu_display = false;
	
	function context_menu_products(obj, datas) {
		if(!$("#context_menu").length) { $("body").append('<div id="context_menu"></div>'); }	
		context_menu_xml = site_admin + 'm=inventory&s=ajax.php&uri=context_menu_products&keywords='+obj.value+'&datas='+datas;
		context_menu_objname = obj.name;
		context_menu_display = true;
		var pos = getpos(obj);
		$('#context_menu').html('');
		$('#context_menu').load(context_menu_xml.replace(/ /g, "%20"));
		$("#context_menu").css("left", pos.left);
		$("#context_menu").css("top", pos.top + obj.offsetHeight - 1);
		$('#context_menu').show();
	}

	function context_menu_page(page) {

		$('#context_menu').load(context_menu_xml, {page:page});
	}
	
	function context_menu_filldata(datas) {
		arrs = datas.split(',');
		for(i=0;i<arrs.length;i++){
			t = arrs[i].split('=')
			$('#'+t[0]).val(t[1]);
			if(t[0]=="stocks")
			{
				$('#stocks').html('库存：'+t[1]);
			}
		}
		$('#context_menu').hide("");
	}
	
	$(document).click(function(evt){
		evt = evt || window.event;
		var _target= evt.target || evt.srcElement;
		if(_target.name!="pagelink" && !context_menu_display) $('#context_menu').hide("");
		context_menu_display = false;
	});


	function context_menu_suppliers(obj, datas) {

		if(!$("body #context_menu").length) { $("body").append('<div id="context_menu"></div>'); }	
		
		context_menu_xml = site_admin + 'm=inventory&s=ajax.php&uri=context_menu_suppliers&keywords='+obj.value+'&datas='+datas;
		context_menu_objname = obj.name;
		context_menu_display = true;

		var pos = getpos(obj);
		$('#context_menu').html('');
		$('#context_menu').load(context_menu_xml.replace(/ /g, "%20"));
		$("#context_menu").css("left", pos.left);
		$("#context_menu").css("top", pos.top + obj.offsetHeight - 1);
		$('#context_menu').show();
	}


	function context_menu_customers(obj, datas) {

		if(!$("body #context_menu").length) { $("body").append('<div id="context_menu"></div>'); }	
		context_menu_xml = site_index + 'admin/ajax/context_menu_customers?keywords='+obj.value+'&datas='+datas;
		context_menu_objname = obj.name;
		context_menu_display = true;

		var pos = getpos(obj);
		$('#context_menu').html('');
		$('#context_menu').load(context_menu_xml.replace(/ /g, "%20"));
		$("#context_menu").css("left", pos.left);
		$("#context_menu").css("top", pos.top + obj.offsetHeight - 1);
		$('#context_menu').show();
	}



	/* ----------------------------
		飘窗功能
	---------------------------- */
	var $backurl = new Array();
	
	function winrun(module) {
		if(!$("#act_window").length) {
			$("body").append('<div id="act_window"></div>');
			var dd = new YAHOO.util.DD("act_window");
			$("#act_window").css("left", document.body.clientWidth/2 - 320);
		}	
		//Nav History
		$backurl[0] = $backurl[1];
		$backurl[1] = module;

		$('#act_window').html('<div id="loading"></div>');
		$('#act_window').load(site_index + 'admin/ajax/' + module, function(){ if(dd)dd.setHandleElId("act_title"); });
		$('#act_window').show();
	}
	
	function winpage(page) {
		$('#act_window').load(site_index + 'admin/ajax/' + $backurl[1], {page:page});
	}
	
	function winclose() {
		$('#act_window').hide();
	}
	
	function winback() {
		winrun($backurl[0]);
	}
	
	function winget(frmobj, module) {
		ipts = frmobj.getElementsByTagName ("input");
		var data = "?";
		for(i=0;i<ipts.length;i++) if(ipts[i].name) data += ipts[i].name+"="+ipts[i].value+"&";
		data = data.substr(0, data.length-1);
		winrun(module + data);
		return false;
	}




	/* ----------------------------
		日历功能
	---------------------------- */
	YAHOO.namespace("example.calendar");
	YAHOO.example.calendar.init = function() {
		YAHOO.example.calendar.cal = new YAHOO.widget.Calendar("cal", "calContainer", { close:true } );
		YAHOO.example.calendar.cal.render();
		YAHOO.util.Event.addListener("sdate", "click", YAHOO.example.calendar.cal.show, YAHOO.example.calendar.cal, true);
		YAHOO.util.Event.addListener("edate", "click", YAHOO.example.calendar.cal.show, YAHOO.example.calendar.cal, true);
		YAHOO.util.Event.addListener("edate1", "click", YAHOO.example.calendar.cal.show, YAHOO.example.calendar.cal, true);
		YAHOO.util.Event.addListener("edate2", "click", YAHOO.example.calendar.cal.show, YAHOO.example.calendar.cal, true);
		YAHOO.util.Event.addListener("sdate1", "click", YAHOO.example.calendar.cal.show, YAHOO.example.calendar.cal, true);
		YAHOO.util.Event.addListener("sdate2", "click", YAHOO.example.calendar.cal.show, YAHOO.example.calendar.cal, true);
		YAHOO.util.Event.addListener("sdate3", "click", YAHOO.example.calendar.cal.show, YAHOO.example.calendar.cal, true);
		YAHOO.util.Event.addListener("edate3", "click", YAHOO.example.calendar.cal.show, YAHOO.example.calendar.cal, true);
		YAHOO.example.calendar.cal.selectEvent.subscribe(handleSelect, YAHOO.example.calendar.cal, true); 
	}
	YAHOO.util.Event.onDOMReady(YAHOO.example.calendar.init);

	var c_input = "";
	
	function lockinput(obj) {
		c_input = obj;
		c_input.select();
		var pos = getpos(obj);
		$("#calContainer").css("left", pos.left);
		$("#calContainer").css("top", pos.top + obj.offsetHeight - 1);
	}
	
	function handleSelect(type, args, obj) {   
		var dates = args[0];   
		var date = dates[0];   
		var year = date[0], month = date[1], day = date[2];   
		c_input.value = year + "-" + month + "-" + day;
		YAHOO.example.calendar.cal.hide();
	}   


	
	//价格设定
	
	function setPriceline(p1,p2){
		$('#priceline').val(p1);
		$('#priceunderline').val(p2);
	}
	
	$(function(){
		/*alert($('[update_price]').size()
				
		)*/
		$('[update_price]').change(function(){
			var pricebuy=parseFloat($('#pricebuy').val());
			if(!pricebuy)return;
			var $priceline=$('#priceline');
			var $punderline=$('#priceunderline');
			var $priceundersell=$('#priceundersell');
			var $pricesell=$('#pricesell');
			switch ($(this).attr('id')){
			
			case 'pricebuy':
			case 'priceunderline':
			case 'priceline':
				$priceundersell.val(($punderline.val()*pricebuy/100+pricebuy).toFixed(2))
				$pricesell.val(($priceline.val()*pricebuy/100+pricebuy).toFixed(2));
			break;
			
			case 'priceundersell':
			
			case 'pricesell':
				$punderline.val((($priceundersell.val()-pricebuy)*100/pricebuy).toFixed(2));
				$priceline.val((($pricesell.val()-pricebuy)*100/pricebuy).toFixed(2));
				
			break;
			}

			
			
			
		});
	    /* 全选 */
	     $('.checkall').click(function(){
	        var _self = this;
	        $('.checkitem').each(function(){
	            if (!this.disabled)
	            {
	                $(this).prop('checked', _self.checked);
	            }
	        });
	        $('.checkall').prop('checked', this.checked);
	    });
	});
	

	//ajax获取采购单SKU列表
	window._flag = false;
	function show_sku_list(purchasesId){
		var _this = $('#purchases_'+purchasesId);
		if(_flag){
			return;
		}
		_flag = true;
		if(_this.attr('act')=='show'){
			$.get('?m=inventory&s=purchases.php',{'purchases_id':purchasesId,'uri':'getPurchasesProductList'},function(res){
				_this.parent().parent().after(res);
				_this.html('关闭');
				_this.attr('act','hide');
				_flag = false;
			});
		}
		if(_this.attr('act')=='hide'){
			_this.parent().parent().next().remove();
			_this.html('查看');
			_this.attr('act','show');
			_flag = false;
		}
	}
	
	//ajax获取调拨单SKU列表
	window._flag = false;
	function show_moving_paper_sku_list(Id){
		var _this = $('#paper_'+Id);
		if(_flag){
			return;
		}
		_flag = true;
		if(_this.attr('act')=='show'){
			$.get('?m=inventory&s=moving.php',{'paper_id':Id,'uri':'getProductList'},function(res){
				_this.parent().parent().after(res);
				_this.html('关闭');
				_this.attr('act','hide');
				_flag = false;
			});
		}
		if(_this.attr('act')=='hide'){
			_this.parent().parent().next().remove();
			_this.html('查看');
			_this.attr('act','show');
			_flag = false;
		}
	}
	
	//排序
	function doorder(key,value){
		var order = $('#order');
		order.attr('name',key);
		order.attr('value',value);
		
		$('#search').trigger('click');
	}
	
	$(function(){
		$('#content').delegate('.purchases_price','click',function(){
			var _this = $(this);
			var pnode = _this.parent().parent();
			var value = _this.text();
			var price = window.prompt("请输入单价",value);
			var purchases_id = pnode.attr('purchases_id');
			if(price>0){
					$.post('?m=inventory&s=purchases.php&uri=updatePrice',{purchases_id:purchases_id,price:price},function(res){
						if(res.code!='0'){
							alert(res.msg);
						}else{
							window.location.reload();
						}
					},'json');
			}
		});
		$('#content').delegate('.purchases_product_discount','click',function(){
			var _this = $(this);
			var pnode = _this.parent().parent();
			var value = _this.text();
			var discount = window.prompt("请输入该商品总折扣",value);
			var purchases_id = pnode.attr('purchases_id');
			if(discount>0){
					$.post('?m=inventory&s=purchases.php&uri=updatePrice',{purchases_id:purchases_id,discount:discount},function(res){
						if(res.code!='0'){
							alert(res.msg);
						}else{
							window.location.reload();
						}
					},'json');
			}
		});
		//查看采购历史
		$('.history_button').click(function(){
			var productid = $(this).attr('productid');
			var shopid = $(this).attr('shopid');
			window.parent.iframe_form("purchases", '历史采购', 'module.php?m=inventory&s=historyPurchases.php&id='+productid+'&shopid='+shopid, 1000, 600);
			return false;
		});
	});
	
	//显示添加调拨单商品窗口
	function showAddMoving(paperId){
		if(paperId>0){
			window.parent.iframe_form("moving", '添加调拨单产品', 'module.php?m=inventory&s=moving.php&uri=showAddMoving&paperId='+paperId, 1000, 600);
		}else{
			alert('调拨单ID错误！');
		}
		return false;
	}
	
	/**
	 * 修改调拨单商品数量
	 * @param dataId
	 */
	function saveMovingProNum(dataId){
		if(dataId>0){
			var num = $('#moving_'+dataId).val();
			$.post('module.php?m=inventory&s=moving.php&uri=settingProductList',{"id":dataId,"number":num,"action":'saveNum'},function(s){
				window.location.reload();
			});
		}
	}
	